# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/topics/item-pipeline.html

from scrapy.exceptions import DropItem

from __init__ import *
from kateglo_crawler.items import DictionaryItem

class DictionaryValidatePipeline(object):
    def process_item(self, item, spider):
        #pipeline onliy for DictionaryItem
        if not isinstance(item, DictionaryItem):
            return item

        #not detailed yet
        if item['is_detailed'] == 0:
            status = "ok"
            if len(item['phrase']) <= 0:
                status = "err: no phrase"
            elif len(item['urllist']) <= 0:
                status = "err: no url"

            if status == "ok":
                # print "%s" % (item['phrase'])
                return item
            else:
                print "%s from %s" % (status, item['urllist'])
                raise DropItem("Drop false phrase: %s" % item['phrase'])
        #after detailed
        else:
            status = "ok"
            if len(item['definition']) <= 0:
                status = "err: no definition"
            elif len(item['class_phrase']) <= 0:
                status = "err: no class"

            if status == "ok":
                # print "%s" % (item['phrase'])
                return item
            else:
                print "%s: %s" % (status, item['phrase'])
                raise DropItem("Drop false phrase: %s" % item['phrase'])

class DictionaryCheckDuplicatePipeline(object):
    def process_item(self, item, spider):
        #pipeline onliy for DictionaryItem
        if not isinstance(item, DictionaryItem):
            return item

        #item not detailed yet
        if item['is_detailed'] == 0:
            global conn

            cursor = conn.cursor()
            query = """
                SELECT id
                FROM dictionary
                WHERE phrase = %s
            """
            cursor.execute(query, (item['phrase']))
            result = cursor.fetchone()

            if result != None and len(result) > 0:
                print "duplicate: %s" % (item['phrase'])
                raise DropItem("Drop duplicate item: %s" % result[0])
            else:
                return item
        #item detailed
        else:
            return item

class DictionaryDbPipeline(object):
    def process_item(self, item, spider):
        #pipeline onliy for DictionaryItem
        if not isinstance(item, DictionaryItem):
            return item

        global conn
        cursor = conn.cursor()

        #item not detailed yet
        if item['is_detailed'] == 0:
            query = """
                INSERT INTO dictionary(phrase, urllist)
                VALUES (%s, %s)
            """
            cursor.execute(query, (item['phrase'], item['urllist']))
        #item after detailed
        else:
            query = """
                UPDATE dictionary SET definition=%s, class=%s, root=%s, source=%s, urldetail=%s, is_detailed=1
                WHERE phrase=%s
            """
            cursor.execute(query, (item['definition'], item['class_phrase'], item['root'], item['source'], item['urldetail'], item['phrase']))

        # Print to screen
        print "%s" % (item['phrase'])

        return item

class AbbrevationValidatePipeline(object):
    def process_item(self, item, spider):
        #pipeline onliy for DictionaryItem
        if not spider.name == 'abbrevation-crawler':
            return item

        status = "ok"
        if len(item['short']) <= 0:
            status = "err: no abbrevation"

        if status == "ok":
            # print "%s" % (item['short'])
            return item
        else:
            print "%s" % (status)
            raise DropItem("Drop false abbrevation")

class AbbrevationCheckDuplicatePipeline(object):
    def process_item(self, item, spider):
        #pipeline onliy for DictionaryItem
        if not spider.name == 'abbrevation-crawler':
            return item

        global conn

        cursor = conn.cursor()
        query = """
            SELECT id
            FROM abbrevation
            WHERE short = %s
        """
        cursor.execute(query, (item['short']))
        result = cursor.fetchone()

        if result != None and len(result) > 0:
            print "duplicate: %s" % (item['short'])
            raise DropItem("Drop duplicate item: %s" % result[0])
        else:
            return item

class AbbrevationDbPipeline(object):
    def process_item(self, item, spider):
        #pipeline onliy for DictionaryItem
        if not spider.name == 'abbrevation-crawler':
            return item

        global conn
        cursor = conn.cursor()

        query = """
            INSERT INTO abbrevation(short, longid, longen, tag, notes, urlsource)
            VALUES (%s, %s, %s, %s, %s, %s)
        """
        cursor.execute(query, (item['short'], item['long_id'], item['long_en'], item['tag'], item['notes'], item['urlsource']))

        # Print to screen
        print "%s" % (item['short'])

        return item